<?php
session_start();

if (!$_SESSION['loggedin']) {
    // User not logged in, redirect to login page
    echo "<script language='javascript'>window.location='index.php'</script>";
}
if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'registrar') {
?>
    <html>
        <head>    
            <link rel="stylesheet" href="css/style.css" type="text/css" id="" media="print, projection, screen" />
            <script type="text/javascript" src="script/jquery-latest.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.pager.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    // call the tablesorter plugin
                    $("table").tablesorter({
                        // sort on the first column and third column, order asc
                        sortList: [[1,0]],
                        headers: {
                            // assign the secound column (we start counting zero)
                            0: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            7: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            8: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            }
                        }
                        ,
                        widthFixed: true, widgets: ['zebra']
                    })
                    .tablesorterPager({container: $("#pager")});
                    ;
                });

                function confirmLogout()
                {
                    var agree=confirm("Are you sure you want to Logout?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
                function confirmAssign()
                {
                    var agree=confirm("Are you sure you want to assign selected professor in this course?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
                function confirmRemove()
                {
                    var agree=confirm("Are you sure you want to remove selected professor in this course?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }

            </script>

            <title>TUP Course Management</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>

        <body style=" height:100%; width: 100%; background-image: url(image/wall6.jpg);">

            <div id="container" style="height:auto; width: 100%;">

                <div style="background-image: url(image/wall12.jpg); height: 110px; width: auto; ">
                </div>
                <div align="center" style="background-image: url(image/wall11.jpg);  height: 74px; width: auto;">
                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="TMMViewProf.php"><b>Professor Management</b></a>&nbsp;&nbsp;
                        <a href="SMMViewStud.php"><b>Student Management</b></a>&nbsp;&nbsp;
                        <a href="CMMViewCourse.php"><b>Course Management</b></a>&nbsp;&nbsp;
                        <a href="EMSelectProf.php"><b>[ Enrollment ]</b></a>
                    </div>

                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="EMSelectProf.php" >Professor Select</a>&nbsp;
                        <a href="EMDeselectProf.php" >Remove Assignment</a>&nbsp;
                        <a  href="EMEnrollStud.php" >Enroll Student</a>&nbsp;
                        <a  href="EMSelectSection.php" >Drop Students by Section</a>&nbsp;
                    </div>

                    <div  align="left" style="padding-left: 4%; height: 18px; width: 46%;float:left;"><?php echo "<aa class=\"smallw\"><b>Welcome " . $_SESSION['fname'] . ", Last login: " . $_SESSION['lastlogin'] . "</b></aa>"; ?>
                    </div>
                    <div align="right" style="padding-right: 4%; height: 18px; width: 46%;float:left;"><bb>|</bb>
                        <a class="small" href="RegEditProfile.php"><b>Edit Profile</b></a>
                        <bb>|</bb>
                        <a onclick="return confirmLogout()" class="small" href="Logout.php"><b>Logout</b></a>
                        <bb>|</bb></div>
                </div>

                <div align="center" style="  float:left; height:auto; width: 100%;">

                    <h2>Assign Courses</h2>

                <?php
                //            $querycourse="SELECT codesecsemyear, teacher, coursetitle, coursecode, description, section, minnumstud, manumstud, totalstud, room, semester, year, starttime, endtime, units, profidno,fname, mname, lname FROM coursetable c LEFT JOIN user u ON c.teacher=u.profidno";

                $prof = $_POST['prof'];
                //echo $prof;

                if (isset($_POST['Remove']) && $prof != null) {
                    $codesecsemyear = $_POST['codesecsemyear'];
                    //echo $codesecsemyear;

                    include 'DBLibrary/config.php';
                    include 'DBLibrary/openDB.php';

                    mysql_select_db($dbname);
                    $updatequery = "update coursetable set teacher='blank' where codesecsemyear='" . $codesecsemyear . "'";

                    $queryvalue = mysql_query($updatequery);
                    if ($queryvalue == 0) {
                        echo "<a class=\"red\">" . mysql_errno($conn) . ": " . mysql_error($conn) . "</a>\n";
                        echo "<h3 class=\"err\">Remove assignment failed</h3>";
                    } else if ($queryvalue == 1)
                        echo "<h3 class=\"err\">Assignment has been removed</h3>";
                    include 'DBLibrary/closeDB.php';
                }

                if (isset($_POST['Assign']) && $prof != null) {
                    $codesecsemyear = $_POST['codesecsemyear'];
                    //echo $codesecsemyear;

                    include 'DBLibrary/config.php';
                    include 'DBLibrary/openDB.php';

                    mysql_select_db($dbname);
                    $updatequery = "update coursetable set teacher='" . $prof . "' where codesecsemyear='" . $codesecsemyear . "'";

                    $queryvalue = mysql_query($updatequery);
                    if ($queryvalue == 0) {
                        echo "<a class=\"red\">" . mysql_errno($conn) . ": " . mysql_error($conn) . "</a>\n";
                        echo "<h3 class=\"err\">Assignment failed</h3>";
                    } else if ($queryvalue == 1)
                        echo "<h3 class=\"err\">Assignment successful</h3>";
                    include 'DBLibrary/closeDB.php';
                }

                include 'DBLibrary/config.php';
                include 'DBLibrary/openDB.php';
                mysql_select_db($dbname);

                $getprofquery = "select * from user where idno='" . $prof . "'";
                $getprofname = mysql_fetch_array(mysql_query($getprofquery));

                echo "<h3>" . $getprofname['lname'] . ",&nbsp;" . $getprofname['fname'] . "&nbsp;" . $getprofname['mname'] . ".</h3>";

                $sql = "SELECT codesecsemyear, teacher, coursetitle, coursecode, description, section, minnumstud, manumstud, totalstud, room, semester, year, starttime, endtime, units, idno,fname, mname, lname FROM coursetable c LEFT JOIN user u ON c.teacher=u.idno WHERE coursetitle LIKE '%" . $name . "%' OR coursecode LIKE '%" . $name . "%' OR teacher LIKE '%" . $name . "%' order by coursecode";

                //-run the query against the mysql query function
                $result = mysql_query($sql);
                ?>
                <table  border="0" id="myTable" class="tablesorter" style="width: 95%;" align="center">
                    <thead><tr>
                            <th>No</th>
                            <th>Course Code</th>
                            <th>Course Section</th>
                            <th>Professor</th>
                            <th>Min Students</th>
                            <th>Max Students</th>
                            <th>Total Students</th>
                            <th>Assign</th>
                            <th>Remove</th>
                        </tr></thead>
                    <tfoot><tr>
                            <th>No</th>
                            <th>Course Code</th>
                            <th>Course Section</th>
                            <th>Professor</th>
                            <th>Min Students</th>
                            <th>Max Students</th>
                            <th>Total Students</th>
                            <th>Assign</th>
                            <th>Remove</th>
                        </tr></tfoot>
                    <tbody>

                        <?php
                        while ($row = mysql_fetch_array($result)) {
                            $ctr++;
                            //if()
                            //echo "<tr>" . "<td>" . $ctr . "</td>";<th>No.</th>
                            echo "<tr><td>" . $ctr . "</td><td>" . $row['coursecode'] . "</td>";
                            echo "<td>" . $row['section'] . "</td>";

                            if ($row['teacher'] != "blank")
                                echo "<td>" . $row['lname'] . ",&nbsp;" . $row['fname'] . "&nbsp;" . $row['mname'] . "</td>";
                            else
                                echo "<td>" . $row['teacher'] . "</td>";

                            echo "<td>" . $row['minnumstud'] . "</td>";
                            echo "<td>" . $row['manumstud'] . "</td>";
                            echo "<td>" . $row['totalstud'] . "</td>";

                            if ($row['teacher'] == "blank") {
                                echo "<td><form onsubmit=\"return confirmAssign()\" name=\"add\" method=\"post\" action=\"\"><input type=\"text\" name=\"prof\" size=\"1\" value=\"" . $prof . "\" style=\"visibility:hidden;\"/>
                               <input type=\"submit\" name=\"Assign\" value=\"Assign\"/><input type=\"text\" name=\"codesecsemyear\" size=\"1\" value=\"" . $row['codesecsemyear'] . "\" style=\"visibility:hidden;\"/></td></form>\n";
                            }
                            else
                                echo "<td></td>\n";

                            if ($row['teacher'] != "blank" && $row['teacher'] == $prof) {
                                echo "<td><form onsubmit=\"return confirmRemove()\" name=\"add\" method=\"post\" action=\"\"><input type=\"text\" name=\"prof\" size=\"1\" value=\"" . $prof . "\" style=\"visibility:hidden;\"/>
                               <input type=\"submit\" name=\"Remove\" value=\"Remove\"/><input type=\"text\" name=\"codesecsemyear\" size=\"1\" value=\"" . $row['codesecsemyear'] . "\" style=\"visibility:hidden;\"/></td></form></tr>\n";
                            }
                            else
                                echo "<td></td></tr>\n";
                        }
                        ?> </tbody></table> <?php
                        ?>


                        <div id="pager" class="pager" align="center" style="float:left; height: auto; width: 100%;">
                            <form>
                                <img src="addons/pager/icons/first.png" class="first"/>
                                <img src="addons/pager/icons/prev.png" class="prev"/>
                                <input readonly="readonly" type="text" class="pagedisplay"/>
                                <img src="addons/pager/icons/next.png" class="next"/>
                                <img src="addons/pager/icons/last.png" class="last"/>
                                <select class="pagesize">
                                    <option selected="selected"  value="10">10</option>

                                    <option value="20">20</option>
                                    <option value="30">30</option>
                                    <option  value="40">40</option>
                                </select>
                            </form>
                            <div id="footer" style="background-image: url(image/wall11.jpg);clear:both;text-align:center; height: 26px; width:auto;">
                                <div align="center" style=" height: 4px; width: auto;"></div>
                                <bb class ="small">Copyright &#169; 2011</bb>
                            </div>
                        </div>
                        <br>

                    </div>
                </div>

            </body>
        </html>
<?php
                    } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'admin') {
                        echo "<script language='javascript'>window.location='UMMViewUser.php'</script>";
                    } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'professor') {
                        echo "<script language='javascript'>window.location='AMMSelectSection.php'</script>";
                    }
?>